<?php
$db = new Database;
if(isset($_POST) and $_SERVER['REQUEST_METHOD']=="POST"){
    $companyName = $_POST['companyName'];
    $companyCode = $_POST['companyCode'];
    $contactNumber = $_POST['contactNumber'];
    $emailAddress = htmlentities($_POST['emailAddress']);
    $primaryContact = $_POST['primaryContact'];
    $companyAddress = $_POST['companyAddress'];
    $captcha = $_POST['captcha-code'];
    $regCap = $_SESSION['captcha_number'];
    $password = $_POST['cpassword'];
    $confirmPassword = $_POST['confirmpassword'];
    
    // For Uploading Image
    $file = $_FILES['file']['name']; // File name
    $size = $_FILES['file']['size']; // File Size
    $path = "images/avatar/";
    $valid_formats = array("jpg", "png");
    list($txt, $ext) = explode(".", $file);
                    
    
    //echo $companyName." ".$companyCode." ".$contactNumber." ".$emailAddress." ".$primaryContact." ".$companyAddress;
    $numrowsComp = $db->query("SELECT * FROM tbcompany where company_code = '$companyCode' and is_active=1","numrows");
    if(empty($companyName) or empty($contactNumber) or empty($emailAddress) or empty($primaryContact) or empty($companyAddress) or empty($password) or empty($confirmPassword)){
       echo "Please Complete all fields";
    }else{
        if(!strlen($file)){
            echo "Please select a file.";
        }else{
            if(!in_array($ext,$valid_formats)){
                echo "Invalid File Format";
            }else{
                if (($size >= 2097152*2097152)){
                    echo "File too large. File must be less than 4 megabytes.";
                }else{
                    if($password!=$confirmPassword){
                        echo "Your Password Didn't Match.";
                    }else{
                        if($numrowsComp>0){
                            echo "Your Company code is Invalid, Try Again!";
                        }else{
                            if(!VerifyMailAddress($emailAddress)){
                                echo "Please type your correct email format.";
                            }else{
                                //if($captcha!=$regCap){
                                //    echo "Captcha code doesnt match.";
                                //}else{
                                
                                    $insertComp = array(
                                                "company_name"=>$companyName,
                                                "primary_contact"=>$primaryContact,
                                                "email_address"=>$emailAddress,
                                                "contact_number"=>$contactNumber,
                                                "company_code"=>$companyCode,
                                                "is_active"=>1,
                                                );
                                    echo "true";
                                    $companyID = $db->insert("tbcompany",$insertComp);
                                    
                                    // Insert to the tbuser
                                    $insertUserComp = array("firstname"=>$companyName,
                                                            "username"=>$emailAddress,
                                                            "password"=>md5($password),
                                                            "company_id"=>$companyID,
                                                            "userlevel_id"=>1,
                                                            "status"=>1,
                                                            "is_active"=>1,
                                                            "role_type"=>1,
                                                            "position_id"=>1,
                                                            "email"=>$emailAddress,
                                                            "role"=>$companyName,
                                                            "extension"=>$ext);
                                    $userID = $db->insert("tbuser",$insertUserComp);
                                    
                                    // Create Folder of the user for their image
                                    mkdir($path."/".$userID); // location
                                        mkdir($path."/".$userID."/1"); // location
                                        mkdir($path."/".$userID."/2"); // location
                                    
                                    // Resize an image
                                       if($ext=="jpg" || $ext=="jpeg" || $ext=="JPG"){
                                            $uploadedfile = $_FILES['file']['tmp_name'];
                                            $src = imagecreatefromjpeg($uploadedfile);
                                       }
                                       else if($ext=="png" || $ext=="PNG"){
                                            $uploadedfile = $_FILES['file']['tmp_name'];
                                            $src = imagecreatefrompng($uploadedfile);
                                       }
                                       else{
                                            $src = imagecreatefromgif($uploadedfile);
                                       }
                                       echo $scr;
                                       
                                       list($width,$height)=getimagesize($uploadedfile);
                                       // Set 1
                                       $newwidth=100;
                                       $newheight=($height/$width)*$newwidth;
                                       $tmp=imagecreatetruecolor($newwidth,$newheight);
                                       
                                       imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                                       
                                       $thumbnail = $path."/".$userID."/"."1/yourAvatar_".$userID.".".$ext;
                                       
                                       imagejpeg($tmp,$thumbnail,100);
                                       
                                       // Set 2
                                       $newwidth1=60;
                                       $newheight1=($height/$width)*$newwidth1;
                                       $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
                                       
                                       imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
                                       
                                       $thumbnail1 = $path."/".$userID."/"."2/yourAvatar2_".$userID.".".$ext;
                                       
                                       imagejpeg($tmp1,$thumbnail1,100);
                                       
                                       imagedestroy($src);
                                       imagedestroy($tmp);
                                       imagedestroy($tmp1);
                                //}
                            }
                        }
                    }   
                }
            }
        }
        
    }
}
?>